﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace GhostStory.Model
{
    class Model
    {
        Player m_player = new Player();
        List<Enemy> m_enemyList = new List<Enemy>();
        Level m_level = new Level(GhostStory.Model.Level.CurrentLevel.LEVEL_1);
        const float m_BottomOfLevel = 9;
        bool m_tookCoin = false;
        public bool once = true;
        private bool m_completed = false;
        //const float m_RightOfLevel = 20;

        bool m_hasCollidedWithGround = false;

        public Model()
        {
            
        }

        public void addEnemies()
        {
            m_enemyList.Clear();
            foreach (Vector2 v in m_level.getSpawn())
                m_enemyList.Add(new Enemy(v));
        }

        internal bool CanJump()
        {
            return m_hasCollidedWithGround;
        }

        internal bool CanWalkRight()
        {
            return true;
        }

        internal bool CanWalkLeft()
        {
            if (m_player.getPosition().X < 0)
            {
                return false;
            }
            return true;
        }

        internal void DoJump()
        {
            m_player.DoJump();
        }

        internal void WalkRight()
        {
            m_player.setSpeed(5, m_player.getSpeed().Y);
        }

        internal void WalkLeft()
        {
            m_player.setSpeed(-5, m_player.getSpeed().Y);
        }

        internal void Stop()
        {
            m_player.Stop();
        }

        internal bool DidPlayerTakeCoin()
        {
            bool tmp = m_tookCoin;
            m_tookCoin = false;
            return tmp;
        }

        internal Level GetLevel()
        {
            return m_level;
        }

        internal void Update(float a_elapsedTime, IObserver a_Observer)
        {
            Vector2 oldPos = m_player.getPosition();

            m_player.Update(a_elapsedTime);
            Vector2 newPos = m_player.getPosition();

            m_hasCollidedWithGround = false;
            Vector2 speed = m_player.getSpeed();
            Vector2 afterCollidedPos = Collide(oldPos, newPos, m_player.m_sizes, ref speed, ref m_hasCollidedWithGround);
            
            m_player.setPosition(afterCollidedPos.X, afterCollidedPos.Y);
            m_player.setSpeed(0, speed.Y);


            foreach (Enemy e in m_enemyList)
            {
                oldPos = e.getPosition();
                e.Update(a_elapsedTime);
                newPos = e.getPosition();

                //m_hasCollidedWithGround = false;
                speed = e.getSpeed();
                //afterCollidedPos = Collide(oldPos, newPos, m_player.m_sizes, ref speed, ref m_hasCollidedWithGround);
                afterCollidedPos = EnemyCollide(oldPos, newPos, e.m_sizes, ref speed);
                e.setPosition(e.getPosition().X, afterCollidedPos.Y);
                e.setSpeed(speed.X, speed.Y);
            }

            //playerKilledEnemy();
                



            //if (m_player.getPosition().X > m_RightOfLevel - 1)
            //{
            //    m_player.setPosition(m_RightOfLevel - 1, m_player.getPosition().Y);
            //    m_player.setSpeed(m_player.getSpeed().X, 0);
            //}
        }

        internal Microsoft.Xna.Framework.Vector2 GetPlayerPosition()
        {
            return m_player.getPosition();
        }

        internal void setNextLevel()
        {
            m_level.setNextLevel();
        }

        private Vector2 Collide(Vector2 a_oldPos, Vector2 a_newPos, Vector2 a_size, ref Vector2 a_velocity, ref bool a_outCollideGround)
        {
            int x = 0, y = 0;
            if (m_level.IsCollidingAt(a_newPos, a_size, Level.Tile.T_EGG, ref x , ref y))
            {
                m_level.m_tiles[x, y] = Level.Tile.T_EMPTY;
                m_player.addPoints(10);
                m_tookCoin = true;
            }


            if (m_level.IsCollidingAt(a_newPos, a_size, Level.Tile.T_BLACKHOLE, ref x, ref y))
            {
                
                m_completed = true;
            }

            if (m_level.IsCollidingAt(a_newPos, a_size, Level.Tile.T_BLOCKED, ref x, ref y))
            {
                
                a_outCollideGround = true;
                
                Vector2 xMove = new Vector2(a_newPos.X, a_oldPos.Y);
                if (m_level.IsCollidingAt(xMove, a_size, Level.Tile.T_BLOCKED, ref x, ref y) == false)
                {

                    
                    a_outCollideGround = true;
                    a_velocity.Y = 0;

                    
                    //a_velocity.X *= 0.25f;


                    return xMove;
                }
                else
                {
                    //try Y movement
                    Vector2 yMove = new Vector2(a_oldPos.X, a_newPos.Y);
                    if (m_level.IsCollidingAt(yMove, a_size, Level.Tile.T_BLOCKED, ref x, ref y) == false)
                    {
                        
                        return yMove;
                    }
                }
                //remain at the same position
                
                return a_oldPos;
            }
            
            return a_newPos;
        }

        private Vector2 EnemyCollide(Vector2 a_oldPos, Vector2 a_newPos, Vector2 a_size, ref Vector2 a_velocity)
        {
            int x = 0, y = 0;
            

            if (m_level.IsCollidingAt(a_newPos, a_size, Level.Tile.T_BLOCKED, ref x, ref y))
            {

                Vector2 xMove = new Vector2(a_newPos.X, a_oldPos.Y);
                if (m_level.IsCollidingAt(xMove, a_size, Level.Tile.T_BLOCKED, ref x, ref y) == false)
                {
                    a_velocity.Y = 0;
                    return xMove;
                }
                else
                {
                    Vector2 yMove = new Vector2(a_oldPos.X, a_newPos.Y);
                    if (m_level.IsCollidingAt(yMove, a_size, Level.Tile.T_BLOCKED, ref x, ref y) == false)
                    {
                        a_velocity.X = a_velocity.X * -1;
                        return yMove;
                        
                    }
                }
                //remain at the same position
                
                return a_oldPos;
            }
            
            return a_newPos;
        }

        internal void whoKilledWho(ref bool died)
        {
            Enemy tmp = null;
            foreach(Enemy e in m_enemyList)
            {
                //if (e.collidedWith(m_player.getPosition(), m_player.m_sizes))
                //
                m_player.getCollider().setPos(new Vector2(m_player.getPosition().X, m_player.getPosition().Y - m_player.getRadius()));
                e.getCollider().setPos(new Vector2(e.getPosition().X, e.getPosition().Y - e.getRadius()));
                if (m_player.getCollider().DoCollide(e.getCollider()))
                {
                    if (m_player.getPosition().Y - 1 < e.getPosition().Y - e.m_sizes.Y)
                    {
                        tmp = e;
                        m_player.setSpeed(m_player.getSpeed().X, (m_player.getSpeed().Y * -1.1f));
                    }

                    else
                        died = true;
                }
                        
            }
            if (tmp != null)
                m_enemyList.Remove(tmp);
            

        }


        internal int getPlayerPoints()
        {
            return m_player.getPoints();
        }

        internal bool isDead()
        {
            if (m_player.getPosition().Y > m_BottomOfLevel)
            {
                //
                return true;
                //
            }
            return false;
        }

        internal bool isClear()
        {
            if (m_completed)
            {
                m_completed = false;
                return true;
                //
            }
            return false;
        }

        internal void RestartGame()
        {
            m_player.setPosition(2, 7);
            m_player.setSpeed(m_player.getSpeed().X, 0);
            m_level = new Level(m_level.getCurrentLevel());
            addEnemies();
            
        }

        internal void setFirstLevel()
        {
            m_level.setFirstLevel();
        }

        internal GhostStory.Model.Level.CurrentLevel getCurrentLevel()
        {
            return m_level.getCurrentLevel();
        }

        internal List<Enemy> getEnemies()
        {
            return m_enemyList;
        }



        internal void resetPoints()
        {
            this.m_player.resetPoints();
        }
    }
}
